//
//  MainViewController.swift
//  01-WeiBo
//
//  Created by 李晓东 on 17/6/24.
//  Copyright © 2017年 PeanutXu. All rights reserved.
//

import UIKit
import SDWebImage

class MainViewController: UITabBarController {

    fileprivate var composedButton:UIButton = UIButton(imageName: "tabbar_compose_icon_add", backImageName: "tabbar_compose_button")
//        {
//        var btn = UIButton()
//        btn.setImage(UIImage.init(named:"tabbar_compose_icon_add") , for: UIControlState.normal)
//        btn.setImage(UIImage.init(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
//        
//        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
//        btn.setBackgroundImage(UIImage.init(named: "tabbar_compose_button"), for: UIControlState.normal)
//        btn.sizeToFit()
//        return btn
//    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        addChildViewControllers()
        setupComposedButton()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.bringSubview(toFront: composedButton)
        
    }
    
    /// dddd
    @objc private func clickComposedButton() {
        print("work")
    }

}

extension MainViewController{
    fileprivate func setupComposedButton(){
        tabBar.addSubview(composedButton)
        let count = childViewControllers.count
        let w = tabBar.frame.width / CGFloat(count) + 1
        composedButton.frame = CGRect(x: w * 2, y: 0, width: w, height: tabBar.bounds.height)
        composedButton.addTarget(self, action: "clickComposedButton", for: UIControlEvents.touchUpInside)
    }
    
    
    fileprivate func addChildViewControllers() {
        tabBar.tintColor = UIColor.orange
        addChildViewController(controller: HomeTableViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(controller: MessageTableViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(UIViewController())
        addChildViewController(controller: DiscoverTableViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(controller: ProfileTableViewController(), imageName: "tabbar_profile", title: "我")
        
    }
    
    private func addChildViewController(controller:UIViewController, imageName:String, title:String) {
//        controller.tabBarItem
        controller.tabBarItem.image = UIImage(named: imageName)
//        controller.tabBarItem.title = title
        controller.title = title
        let nav = UINavigationController()
        nav.addChildViewController(controller)
        
        addChildViewController(nav)
    }

}

